var tableElem = "LAY-home-pendants-list";
var formElem = "LAY-home-pendants-listform";
var tableToolsElem = "LAY-home-pendants-tools";
var formSubmitElem = "LAY-home-pendants-listform-submit";
var formUrl = '/home/system/pendants/listform'
var historyUrl = '/home/system/pendants/list'
var baseUrl = window.globalConfig.url_cms_pendants


layui.use(['common'], function (common) {
    var $ = layui.$
        , admin = layui.admin
        , table = layui.table
    //挂件管理
    var tableItem = table.render({
        elem: `#${tableElem}`
        , url: baseUrl //模拟接口
        , lineStyle: "height:80px"
        , cols: [[
            // {type: 'checkbox', fixed: 'left'}
            {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '挂件名称', editor: 'text'}
            , {field: 'icon', title: '挂件图标', editor: 'text'}
            , {field: 'link', title: '挂件链接', editor: 'text'}
            , {field: 'label', title: '显示文本', editor: 'text'}
            , {field: 'is_show', title: '是否显示', templet: "#ID-table-pendants-is_show-switch"}
            , {field: 'is_blank', title: '跳转新标签', templet: "#ID-table-pendants-is_blank-switch"}
            , {field: 'sort', title: '排序', editor: 'text'}
            , {title: '操作', minWidth: 80, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: {none: '暂无数据！'}
    });

    var active = {
        //添加
        add: function (othis) {
            common.popupSaveHandler('添加挂件', baseUrl, tableElem, formUrl, formElem, formSubmitElem)
        }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
        console.log('a')
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    // 增删改查
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') { // 删除挂件
            layer.confirm('确定删除此挂件？', function (index) {
                admin.req({
                    url: baseUrl + "/" + data.id,
                    type: "DELETE",
                    success: function (res) {
                        common.ctmMsg(res.code,res.msg)
                        tableItem.reloadData(); //重载表格
                    }
                })
            });
        } else if (obj.event === 'edit') {
            common.popupSaveHandler('编辑挂件', baseUrl, tableElem, formUrl, formElem, formSubmitElem, data)
        }
    });

    common.tableSwitchEditor('pendants-is_show-status', baseUrl)
    common.tableSwitchEditor('pendants-is_blank-status', baseUrl)

});